package sucre
 
import ve.org.src.util.DateUtils


class PrincipalController {

    def loginService   


    def index = {
        def accionsMenu = session["seguridad_actionMenu"]
        
        if(!session["seguridad_accionMenuSinDependencia"]){
            def accionesFiltro=[]
            accionsMenu.each{
                if(it.visible){
                    accionesFiltro.add(it)
                }
            }
            session["seguridad_accionMenuSinDependencia"]=accionesFiltro
        }
        
        return [html:buildMenu(session["seguridad_accionMenuSinDependencia"])]

    }
    def inicio={}
    def buildMenu(List ls){
        String html='<ul class="menu">'
        ls.each{
            if(!it.padre) {
                html+=buildChilds(ls,it)
            }
        }
        html+='</ul>'
        return html
    }
    def buildChilds(List ls,AccionMenu accion){
        String html=''
        def childs=findChilds(ls,accion)
        if(childs.size()>0){
            if(!accion.padre){
                html+='<li><a href="#">'+accion.descripcion+'</a>'
            }else{
                html+='<li><a class="parent" href="#"><span>'+accion.descripcion+'</span></a>'
            }
            html+='<ul >'
            childs.each{
                html+=buildChilds(ls,it)
            }
            html+='</ul></li>'
        }else{
            html+='<li class="yui-menuitem"><a class="yui-menuitem-content" style="cursor:pointer;"  target="source" onClick="redirect(\''+accion.accion+'\',\''+accion.controlador+'\','+"\'contenedor\'"+')">'+accion.descripcion+'</a></li>'
            //html+='<li ><a href="../../'+accion.controlador+'/'+accion.accion+'" target="source" onclick="">'+accion.descripcion+'</a></li>'
            
            
        }
        return html
    }
    def findChilds(ls,AccionMenu accion){
        def childs=[]
        ls.each{

            if(it.padre==accion){
                childs.add(it)
            }
        }
        return childs
    }
    def fillName(String name){
        name.replaceAll(' ','-')
    }

}
